import java.util.Arrays;

public class Sort {
    //快速插入法；
    public static void insertSort(int[]array){
        for(int i=1;i< array.length;i++){
            int tmp=array[i];
            int j=i-1;
            for(;j>=0;j--){
                if(array[j]>tmp){
                    array[j+1]=array[j];
                }else{
                    break;
                }
            }
            array[j+1]=tmp;
        }
    }
    public static void main1(String[] args){
        int[] array={12,3,42,8,27,9};
        insertSort(array);
        System.out.println(Arrays.toString(array));
    }
    //希尔排序；
    public static void shellSort(int[] array){
        int gap= array.length;
        while(gap>=1){
            gap=gap/2;
            shell(array,gap);
        }
    }
    public static void shell(int[] array,int gap){
        for(int i=1;i< array.length;i++){
            int tmp=array[i];
            int j=i-gap;
            for(;j>=0;j-=gap){
                if(array[j]>tmp){
                    array[j+gap]=array[j];
                }else{
                    break;
                }
            }
            array[j+gap]=tmp;
        }
    }
    public static void main2(String[] args){
        int array[]={23,44,53,33,22,3};
        shellSort(array);
        System.out.println(Arrays.toString(array));
    }
    //选择排序；
    public static void selectSort(int[] array){
        for(int i=0;i< array.length;i++){
            int minIndex=i;
            for(int j=i+1;j< array.length;j++){
                if(array[j]<array[minIndex]){
                    minIndex=j;
                }
            }
            if(i!=minIndex){
                swap(array,i,minIndex);
            }
        }
    }
    public static void swap(int[] array,int i,int j){
        int tmp=array[i];
        array[i]=array[j];
        array[j]=tmp;
    }
    public static void main3(String[] args){
        int array[]={23,44,53,33,22,3};
        selectSort(array);
        System.out.println(Arrays.toString(array));
    }
    //堆排序；
    public static void heapSort(int[] array){
        for(int parent=((array.length)-1-1)/2;parent>=0;parent--){
            siftDown(array,parent);
        }
    }
    public static void siftDown(int[] array,int parent){
        int son=2*parent+1;
        while(son< array.length){
            while(son+1<array.length&&array[son]<array[son+1]){
                son++;
            }
            if(array[parent]<array[son]){
                swap(array,parent,son);
                parent=son;
                son=2*parent+1;
            }else{
                break;
            }
        }
    }
    public static void main(String[] args){
        int array[]={23,44,53,33,22,3};
        selectSort(array);
        System.out.println(Arrays.toString(array));
    }

}
